package com.qiaoya.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.qiaoya.repository.HealthDataRepository;
import com.qiaoya.service.HealthDataService;
import com.qiaoya.util.HttpRequest;

@Service("Health")
public class HealthDataServiceImpl implements HealthDataService{
	private final HealthDataRepository healthDataRepository;
	
	@Autowired
	public HealthDataServiceImpl(HealthDataRepository healthDataRepository) {
		this.healthDataRepository = healthDataRepository;
	}
	
	@Override
	public Integer insertPulse(String Watch_ID, String Pulse, String CreateTime, int DataType) {
		return healthDataRepository.insertPulse(Watch_ID, Pulse, CreateTime, DataType);
	}

	@Override
	public String insertWifi(String Watch_ID, String Wifi, String CreateTime, int DataType) {
		//传进来的MAC地址要转换为经纬度：1.格式转换 2调高德接口 3.写数据库
    	String LBS2 = Wifi.replaceAll("\\&\\|", "\\|");
    	String LBS3 = LBS2.replaceAll("\\&", ",");
    	//System.out.println(LBS3);
    	String jsonReturn=HttpRequest.sendGet("http://apilocate.amap.com/position", "accesstype=1&imei="+Watch_ID+"&output=json&key=707ffd7841d12d179c99a58c9867067a&macs="+LBS3);
        //System.out.println(jsonReturn);
        JSONObject json = JSONObject.parseObject(jsonReturn); 
		JSONObject json1 = json.getJSONObject("result");
		String location = json1.getString("location");
		int i = healthDataRepository.insertGPS(Watch_ID, location.split(",")[0], location.split(",")[1], CreateTime, DataType);
		return json1.getString("desc");
	}

	@Override
	public Integer insertGPS(String Watch_ID, String AddrInfoX, String AddrInfoY, String CreateTime, int DataType) {
		return healthDataRepository.insertGPS(Watch_ID, AddrInfoX, AddrInfoY, CreateTime, DataType);
	}

	@Override
	public String insertLBS(String Watch_ID, String LBS, String CreateTime, int DataType) {
		String[] LBSArray=LBS.split("\\|", 5);
        //System.out.println(LBSArray[0]);
        int LBSnum =  Integer.valueOf(LBSArray[0]);
        String mcc = LBSArray[1];
        String mnc = LBSArray[2];
        String sLBS = LBSArray[4];
        String[] sLBSArray   = sLBS.split("\\$", LBSnum);
        String bts="";
        String nearbts="";
        for(int i=0;i<LBSnum;i++){
        	String tmp = sLBSArray[i];
        	String[] tmpArray = tmp.split("\\&",4);
        	String ss=mcc+","+mnc+","+String.valueOf(Integer.parseInt(tmpArray[0], 16))+","+String.valueOf(Integer.parseInt(tmpArray[1], 16));
        	if(tmpArray[2].contains("-")){
        		ss=ss+","+tmpArray[2];    		
        	}
        	else{
        		ss=ss+","+String.valueOf((Integer.parseInt(tmpArray[2]))*2-113); 
        	}
        	if(i==0){
        		bts=ss;
        	}
        	else{
        		nearbts=nearbts+"|"+ss;
        	}
        }
        String jsonReturn=HttpRequest.sendGet("http://apilocate.amap.com/position", "accesstype=0&imei="+"888821000001095"+"&cdma="+"0"+"&bts="+bts+"&nearbts="+nearbts+"&output=json&key=707ffd7841d12d179c99a58c9867067a");
        //System.out.println(jsonReturn);
        JSONObject json = JSONObject.parseObject(jsonReturn); 
		JSONObject json1 = json.getJSONObject("result");
		String location = json1.getString("location");
		int i = healthDataRepository.insertGPS(Watch_ID, location.split(",")[0], location.split(",")[1], CreateTime, DataType);
		return json1.getString("desc");
	}

	@Override
	public void insertBloodPress(String imei, String systolic, String diastolic, String strTime, int i) {
		// TODO Auto-generated method stub
		healthDataRepository.insertBloodPress(imei, systolic, diastolic, strTime, i);
	}

	@Override
	public void insertStep(String imei, String strStep, String strTime, int i) {
		// TODO Auto-generated method stub
		healthDataRepository.insertStep(imei,strStep,strTime,i);
		
	}
}
